package com.jjb.common.view;

import sun.font.FontDesignMetrics;

import java.awt.*;
import java.util.ArrayList;

public class DrawUtil {

    /**
     * 绘制一个正方形
     * @param g
     * @param x
     * @param y
     * @param width
     * @param height
     */
    public static void drawRectangle(Graphics2D g, Color color,int x, int y , int width, int height) {
        g.setColor(color);
        g.drawLine(x, y, x + width, y);
        g.drawLine(x, y , x, height + y);
        g.drawLine(x + width, y, x + width, y+height);
        g.drawLine(x, y+height , x+width, y + height);

       /* g.drawLine(x, y + height, x + width, y + table.getLineHeight());
        g.drawLine(x+width, y , x+width, table.getLineHeight() + y);*/
    }

    /**
     * 填充一个矩形
     * @param g
     * @param color
     * @param x
     * @param y
     * @param width
     * @param height
     */
    public static void fillRect(Graphics2D g, Color color,int x, int y , int width, int height) {
        if(color!=null){
            g.setColor(color);
            g.fillRect(x, y,  width, height);
        }
    }

    public static int getWidth(Font font, String content, int space) {
        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        int width = 0 ;
        for(int i =0; i < content.length(); ++i) {
            if( i != 0) {
                width += space;
            }
            width += metrics.charWidth(content.charAt(i));
        }
        return width;
    }

    public static ArrayList<String> getWidthLine(Font font, String content, int width, int space) {
        ArrayList<String>  widthLines = new ArrayList<String>();
        int clength = content.length();
        StringBuffer sb = new StringBuffer("");
        for(int i =0 ; i < clength; ++i) {
            char c = content.charAt(i);

            StringBuffer b = new StringBuffer(sb.toString());
            b.append(c);
            String bcontent = b.toString();
            int w = getWidth(font, bcontent, space);
            if(w <= width) {
                sb.append(c);
            } else {
                widthLines.add(sb.toString());
                sb = new StringBuffer("");
                sb.append(c);
            }
        }

        String lastContent = sb.toString();
        if(lastContent.length() > 0) {
            widthLines.add(sb.toString());
        }
        return widthLines;
    }


    /**
     * 获取绘制文字的高度
     * @param font
     * @param content
     * @param width
     * @param rowSpacing: 行间距
     * @return
     */
    public static int getHeight(Font font, String content, int width, int space, int rowSpacing) {
        ArrayList<String> arrayList = getWidthLine(font, content, width, space);

        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
        int sizeHeight = metrics.getHeight();

        int height = 0;
        for( int i = 0;  i < arrayList.size(); ++i ) {
            if( i != 0  ) {
                height += rowSpacing;
            }
            height += sizeHeight;
        }
        return height;
    }


    public static void drawTextArea(Graphics2D g,Color color, Font font, int x, int y, int width,int rowSpacing,int space,  String content ) {
        ArrayList<String> drawList = DrawUtil.getWidthLine(font, content, width, space);
        int currenty= y;
        for(int i = 0 ; i < drawList.size(); ++i) {
            drawText(g, x, currenty, color, font, space, drawList.get(i));
            currenty += font.getSize() + rowSpacing;
        }
    }

    public static void drawText(Graphics2D g,int x, int y, Color color, Font font,int space, String content) {
        g.setColor(color); //根据图片的背景设置水印颜色
        g.setFont(font);              //设置字体
        int currentX = x;
        int currenty = y;

        for(int i = 0; i < content.length(); ++i) {
            char sc = content.charAt(i);
            String  scontent = sc +"";
            g.drawString(scontent, currentX, currenty);  //画出
            currentX = currentX + getWidth(font, scontent, 0) + space;
        }
    }
}
